package dao;

import entity.AdminEnity;
import entity.UserEnity;
import util.JdbcUtile;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:XIAOXIN
 * @date:2025/04/27
 **/
public class AdminDao {

    public AdminEnity getOneAdminUser() throws SQLException {
        List<UserEnity> list = new ArrayList<>();
        Connection con = JdbcUtile.getConnection();

        String sql = "SELECT id,name,passward\n" +
                "FROM `admin`\n"+"where id=1";
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet set = ps.executeQuery();
        AdminEnity adminEnity = null;
        while (set.next()){
            int id = set.getInt(1);
            String user_name = set.getString(2);
            String user_passward = set.getString(3);
            adminEnity = new AdminEnity(id,user_name,user_passward);
        }
        return adminEnity;
    }

    public boolean updateAdmin(int id, String name, String password) throws SQLException {
            int res = 0;

            Connection con = JdbcUtile.getConnection();
            String sql = "update admin set name=?,passward=? where id=?";
            PreparedStatement ps = con.prepareStatement(sql);

            ps.setString(1,name);
            ps.setString(2,password);
            ps.setInt(3,id);

            res = ps.executeUpdate();
            JdbcUtile.closeConnect(con);
            return res!=0;
        }
}
